package com.blog.web.manage;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.blog.model.Page;
import com.blog.model.ResultJSON;
import com.blog.model.TableJson;
import com.blog.pojo.Article;
import com.blog.pojo.Cate;
import com.blog.service.ArticleService;

/** 
 * @author ant 
 * @date 创建时间：2017年8月17日 上午10:49:14  
 */
@RestController("manageArticleApiController")
@RequestMapping("/manage/article")
public class ArticleApiController {
	
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(value="/cateList",method=RequestMethod.POST)
	public TableJson getCateList(Cate cate,Page page) {
		TableJson tableJson = articleService.getCateList(cate, page);
		return tableJson;
	}
	
	/** 
	 * @Description: 分类编辑的处理 
	 * @param category
	 * @param act 编辑状态：add为添加 ；edit为修改
	 * @return ResultJSON
	 */
	@RequestMapping(value="/cateEdit",method=RequestMethod.POST)
	public ResultJSON editCate(Cate cate,@RequestParam(value="act")String act) {
		ResultJSON json = articleService.editCate(cate, act);
		return json;		
	}
	
	/** 
	 * @Description: 删除分类 
	 * @param cateId
	 * @return ResultJSON
	 */
	@RequestMapping(value="/deleteCate/{cateId}",method=RequestMethod.POST)
	public ResultJSON deleteCate(@PathVariable Integer cateId) {
		ResultJSON json = articleService.deleteCate(cateId);
		return json;		
	}
	
	/**
	 * @Description: 添加文章的处理
	 * @param article
	 * @return ResultJSON
	 */
	@RequestMapping(value="/addArticle",method=RequestMethod.POST)
	public ResultJSON addArticle(Article article,String picture,@RequestParam(value="file",required=false)MultipartFile file) {
		String [] pictureArray = picture.split(",");		
		List<String> pictureList = Arrays.asList(pictureArray);		
		ResultJSON json = articleService.addArticle(article,pictureList,file);
		return json;		
	}

	@RequestMapping(value="/articleList",method=RequestMethod.POST)
	public TableJson getArticleList(Article article,Page page) {
		TableJson tableJson = articleService.getArticleList(article, page);
		return tableJson;
	}
	
	/**
	 * @Description: 添加文章的处理
	 * @param article
	 * @return ResultJSON
	 */
	@RequestMapping(value="/editArticle",method=RequestMethod.POST)
	public ResultJSON editArticle(Article article,String picture,@RequestParam(value="file",required=false)MultipartFile file) {		
		String [] pictureArray = picture.split(",");		
		List<String> pictureList = Arrays.asList(pictureArray);
		ResultJSON json = articleService.editArticle(article,pictureList,file);
		return json;		
	}
}
